<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <span
          style="color: red;">"说明：1、本表财政支出结余应为0，非财政收入中技术服务收入、成果转化收入、科研副产品收入均填报真实测算收入，因预算上报为平衡预算，可与预算收入明细表中的预算收入不一致，本表测算产生的收支结余仅作为后续管理参考依据；
          2、填报举例：维修费：如维修大门3.5万，测算内容：大门维修；测算依据：说明3.5万金额是如何测算出来的；
          3、各单位科室只需测算基本公用经费，与非财政收入相关的经费支出不按科室测算，即各科室（基本）=财政基本+非财政基本；
          4、各项支出科目可按报账规范要求进行具体测算 。"
          <span style="color: red;">单位：万元
          </span>
        </span>
        <a-row>
          <!-- 经济分类科目改为下拉框 -->
          <a-col :span="24">
            <a-form-model-item label="经济分类科目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jflx">
              <a-select v-model="model.jflx" placeholder="请选择经济分类科目" @change="handleJflxChange">
                <a-select-option value="财政">财政</a-select-option>
                <a-select-option value="非财政">非财政</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <!-- 新增项目类型下拉框 -->
          <a-col :span="24">
            <a-form-model-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmlx">
              <a-select v-model="model.xmlx" placeholder="请选择项目类型" :disabled="!isProjectTypeEnabled">
                <a-select-option value="成果转化">成果转化</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmmc">
              <a-input v-model="model.xmmc" placeholder="请输入项目名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="年度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nd">
              <j-dict-select-tag type="list" v-model="model.nd" dictCode="year" placeholder="请选择年度"
                @change="handleYearChange" />
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">-->
          <!--            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dw">-->
          <!--              <a-input v-model="model.dw" placeholder="请输入单位"></a-input>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="办公费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bgf">
              <a-input v-model="model.bgf" placeholder="请输入办公费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="印刷费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ysf">
              <a-input v-model="model.ysf" placeholder="请输入印刷费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手续费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sxf">
              <a-input v-model="model.sxf" placeholder="请输入手续费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="水费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sf">
              <a-input v-model="model.sf" placeholder="请输入水费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="电费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="df">
              <a-input v-model="model.df" placeholder="请输入电费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邮电费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ydf">
              <a-input v-model="model.ydf" placeholder="请输入邮电费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="取暖费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qnf">
              <a-input v-model="model.qnf" placeholder="请输入取暖费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物业管理费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wyglf">
              <a-input v-model="model.wyglf" placeholder="请输入物业管理费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="差旅费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="clf">
              <a-input v-model="model.clf" placeholder="请输入差旅费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="因公出境费用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ygcjf">
              <a-input v-model="model.ygcjf" placeholder="请输入因公出境费用"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维护费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="whf">
              <a-input v-model="model.whf" placeholder="请输入维护费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="租赁费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zlf">
              <a-input v-model="model.zlf" placeholder="请输入租赁费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="会议费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hyf">
              <a-input v-model="model.hyf" placeholder="请输入会议费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="培训费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pxf">
              <a-input v-model="model.pxf" placeholder="请输入培训费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公务接待费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gwjd">
              <a-input v-model="model.gwjd" placeholder="请输入公务接待费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="专用材料费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zycl">
              <a-input v-model="model.zycl" placeholder="请输入专用材料费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="被装购置费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bzgzf">
              <a-input v-model="model.bzgzf" placeholder="请输入被装购置费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="专用燃料费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zyrlf">
              <a-input v-model="model.zyrlf" placeholder="请输入专用燃料费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="劳务费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lwf">
              <a-input v-model="model.lwf" placeholder="请输入劳务费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="委托业务费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wtywf">
              <a-input v-model="model.wtywf" placeholder="请输入委托业务费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工会经费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ghjf">
              <a-input v-model="model.ghjf" placeholder="请输入工会经费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公务用车运行维护费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gwcyc">
              <a-input v-model="model.gwcyc" placeholder="请输入公务用车运行维护费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="其他交通费用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qtjtfy">
              <a-input v-model="model.qtjtfy" placeholder="请输入其他交通费用"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="税金及附加费用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sj">
              <a-input v-model="model.sj" placeholder="请输入税金及附加费用"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="其他商品和服务支出" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qtsp">
              <a-input v-model="model.qtsp" placeholder="请输入其他商品和服务支出"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="房屋建筑物购建" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fw">
              <a-input v-model="model.fw" placeholder="请输入房屋建筑物购建"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="办公设备购置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bg">
              <a-input v-model="model.bg" placeholder="请输入办公设备购置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="专用设备购置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zy">
              <a-input v-model="model.zy" placeholder="请输入专用设备购置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="基础设施建设" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jc">
              <a-input v-model="model.jc" placeholder="请输入基础设施建设"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="大型修缮" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dx">
              <a-input v-model="model.dx" placeholder="请输入大型修缮"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="信息网络及软件购置更新" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xx">
              <a-input v-model="model.xx" placeholder="请输入信息网络及软件购置更新"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物资储备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wz">
              <a-input v-model="model.wz" placeholder="请输入物资储备"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="土地补偿" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="td">
              <a-input v-model="model.td" placeholder="请输入土地补偿"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="安置补助" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="az">
              <a-input v-model="model.az" placeholder="请输入安置补助"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地上附着物和青苗补偿" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dsfzw">
              <a-input v-model="model.dsfzw" placeholder="请输入地上附着物和青苗补偿"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="拆迁补偿" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cq">
              <a-input v-model="model.cq" placeholder="请输入拆迁补偿"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公务用车购置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gwc">
              <a-input v-model="model.gwc" placeholder="请输入公务用车购置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="其他交通工具购置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qtjt">
              <a-input v-model="model.qtjt" placeholder="请输入其他交通工具购置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="文物和陈列品购置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wwcl">
              <a-input v-model="model.wwcl" placeholder="请输入文物和陈列品购置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="无形资产购置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wxzcgz">
              <a-input v-model="model.wxzcgz" placeholder="请输入无形资产购置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="其他资本性支出" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qtzbszc">
              <a-input v-model="model.qtzbszc" placeholder="请输入其他资本性支出"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="基本收入总额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jbsrze">
              <a-input v-model="model.jbsrze" placeholder="请输入基本收入总额"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="当年成果转化收入计提的成果转化奖" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cgzhj">
              <a-input v-model="model.cgzhj" placeholder="请输入当年成果转化收入计提的成果转化奖" :disabled="!isCgzhjEnabled"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { mapState } from 'vuex'
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'CwGyjfszForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        nd: "",
        jflx: "", // 经济分类科目
        xmlx: "", // 项目类型
        cgzhj: "" // 新增：当年成果转化收入计提的成果转化奖
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        jflx: [
          { required: true, message: '请选择经济分类科目', trigger: 'change' }
        ],
        // 新增成果转化奖校验（按需选择，非必需
      },
      url: {
        add: "/cw/cwGyjfsz/add",
        edit: "/cw/cwGyjfsz/edit",
        queryById: "/cw/cwGyjfsz/queryById"
      },
      isManualChange: false, // 标记是否手动修改过表单年度（复用成功代码）
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    // 控制成果转化奖输入框启用状态
    isCgzhjEnabled() {
      return this.model.jflx === '非财政' && this.model.xmlx === '成果转化'
    },
    // 完全复用成功代码的全局年度映射逻辑
    ...mapState({ globalYear: state => state.app.globalYear })

  },
  watch: {
    globalYear(newVal) {
      if (!this.isManualChange) {
        this.model.nd = newVal;
      }
    },
    'model.nd'(newVal, oldVal) {
      if (oldVal && newVal !== this.globalYear) {
        this.isManualChange = true;
      }
    },
    // 监听经济分类科目变化，控制项目类型禁用状态
    'model.jflx'(newVal) {
      this.isProjectTypeEnabled = newVal === '非财政'
      // 如果切换为财政，清空项目类型
      if (newVal !== '非财政') {
        this.model.xmlx = ''
      }
    }
  },
  created() {
    this.model.nd = this.globalYear;
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    // 2. 年度选择变化处理（无需修改逻辑，适配新的年度格式）
    handleYearChange(year) {
      console.log('年度切换为：', year);
    },
    // 经济分类科目变化处理
    handleJflxChange(value) {
      this.isProjectTypeEnabled = value === '非财政'
      if (value !== '非财政') {
        this.model.xmlx = ''
      }
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>